package com.taijihuabao.modules.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.modules.entity.CheckInRuleEntity;
import com.taijihuabao.modules.mapper.CheckInRuleMapper;
import com.taijihuabao.modules.model.checkin.dto.CheckInRuleUpDTO;
import com.taijihuabao.modules.model.checkin.vo.CheckInRuleInfoVO;
import com.taijihuabao.modules.service.CheckInRuleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 考勤规则
 *
 * @author changhui.tu
 */
@Service
public class CheckInRuleServiceImpl implements CheckInRuleService {

    @Resource
    private CheckInRuleMapper checkInRuleMapper;

    @Override
    public CheckInRuleInfoVO selCheckInRule() {
        CheckInRuleEntity checkInRuleEntity = checkInRuleMapper.selCheckInRule();
        CheckInRuleInfoVO checkInRuleInfoVO = new CheckInRuleInfoVO();
        checkInRuleInfoVO.setId(checkInRuleEntity.getId());
        checkInRuleInfoVO.setStartWorkRule(checkInRuleEntity.getStartWorkRule());
        checkInRuleInfoVO.setFinishWorkRule(checkInRuleEntity.getFinishWorkRule());
        checkInRuleInfoVO.setSeriousLateRule(checkInRuleEntity.getSeriousLateRule());
        checkInRuleInfoVO.setAbsenteeismRule(checkInRuleEntity.getAbsenteeismRule());
        checkInRuleInfoVO.setCloseStartWork(checkInRuleEntity.getCloseStartWork());
        checkInRuleInfoVO.setSiestaStart(checkInRuleEntity.getSiestaStart());
        checkInRuleInfoVO.setSiestaFinish(checkInRuleEntity.getSiestaFinish());
        return checkInRuleInfoVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updCheckInRule(CheckInRuleUpDTO ruleUpDTO) {
        if (Objects.isNull(ruleUpDTO.getId())) {
            throw new ServiceException(ApplicationResponseCode.ID_NOTNULL);
        }
        if (StringUtils.isEmpty(ruleUpDTO.getStartWorkRule()) || StringUtils.isEmpty(ruleUpDTO.getFinishWorkRule()) ||
                Objects.isNull(ruleUpDTO.getSeriousLateRule()) || Objects.isNull(ruleUpDTO.getAbsenteeismRule()) ||
                StringUtils.isEmpty(ruleUpDTO.getCloseStartWork()) || StringUtils.isEmpty(ruleUpDTO.getSiestaStart()) ||
                StringUtils.isEmpty(ruleUpDTO.getSiestaFinish())
        ) {
            throw new ServiceException(ApplicationResponseCode.CHECK_IN_RULE_NOTNULL);
        }
        CheckInRuleEntity checkInRuleEntity = new CheckInRuleEntity();
        checkInRuleEntity.setId(ruleUpDTO.getId());
        checkInRuleEntity.setStartWorkRule(ruleUpDTO.getStartWorkRule());
        checkInRuleEntity.setFinishWorkRule(ruleUpDTO.getFinishWorkRule());
        checkInRuleEntity.setSeriousLateRule(ruleUpDTO.getSeriousLateRule());
        checkInRuleEntity.setAbsenteeismRule(ruleUpDTO.getAbsenteeismRule());
        checkInRuleEntity.setCloseStartWork(ruleUpDTO.getCloseStartWork());
        checkInRuleEntity.setSiestaStart(ruleUpDTO.getSiestaStart());
        checkInRuleEntity.setSiestaFinish(ruleUpDTO.getSiestaFinish());
        checkInRuleMapper.delCheckInRule(checkInRuleEntity);
        checkInRuleMapper.addCheckInRule(checkInRuleEntity);
    }

}
